/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import java.io.*;

/**
 *
 * @author Diogo
 */
public class Utils {
    public static final String CAMINHO = System.getProperty("user.dir") + "/SAVE_GAME.obj";
    
    public static void salvarArquivo(Object object, String path){
        
        ObjectOutputStream objectOut = null;
        try{
            objectOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));              
            objectOut.writeObject(object);
        }catch(Exception e){
            System.err.println("ERRO : Utils.salvarArquivo : " + e.getMessage());
        }
        finally{
            closeObject(objectOut);
        }
        
    }
    
    public static SaveGame abrirArquivo(String path){
        
        ObjectInputStream objectIn = null;
        try{
            objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));  
            return (SaveGame)objectIn.readObject();
        }
        catch(Exception e){
            System.err.println("ERRO : Utils.salvarArquivo : " + e.getMessage());
        }
        finally{  
            closeObject(objectIn);
        }
        return null;
        
    }
    
    public static void closeObject(ObjectInputStream objectIn){
        
        try{
            objectIn.close();
        }
        catch(Exception e){
            System.err.println("ERRO : Utils.closeObject : " + e.getMessage());
        }
        
    }
    
    public static void closeObject(ObjectOutputStream objectOut){
        
        try{
            objectOut.close();
        }
        catch(Exception e){
            System.err.println("ERRO : Utils.closeObject : " + e.getMessage());
        }
        
    }
    
}
